package name.pilgr.soaboard.view;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import name.pilgr.soaboard.model.DataService;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public abstract class ObservantPanel extends VerticalLayout implements Observer{
	
	Panel panel;
	//Label panelText;
	DataService dataService;
	
	public ObservantPanel(DataService dataService){
		this.dataService = dataService;
		dataService.addObserver(this);
		
		panel = new Panel("Events dynamics");
		//panelText = new Label("Init...",Label.CONTENT_XHTML);

		//panel.addComponent(panelText);
		panel.setHeight("300px");
		panel.setWidth("300px");
		panel.setSizeFull();
		
		this.setSpacing(false);
		this.setMargin(false);
		
		this.addComponent(panel);
		this.setComponentAlignment(panel, Alignment.BOTTOM_LEFT);
	}
	
	public synchronized void refreshPanel() {
		visualize(dataService.getCurrentData());
	}
	
	abstract void visualize(Vector<Vector<String>> currentData);// {
		// by row in all vector
		/*String curString ="";
		for (int iRow = 0; iRow < currentData.size(); iRow++){
			Vector<String> row = new Vector<String>();
			row = currentData.get(iRow);
		
			// by elements in row
			for (int iElem = 0; iElem < row.size(); iElem ++){
				curString +="|" + row.get(iElem).toString();
			}
			curString += "<br>";
		}
		panelText.setValue(curString);*/
	//}

	public void update(Observable o, Object arg) {
		// Normal flow
		if (arg == null) {
			refreshPanel();
		} else {//arg - Exception
			/*panelText.setValue( "Sorry, but service reported about error: "+
					"<b><i>"+((Exception)arg).getMessage() + "</b></i>.<br>" +
					"Please make sure that the service works and the correct settings to access it");*/
		}
	}	
	
}
